package org.xlp.base.application.service;

import org.xlp.base.application.base.CreatedInfo;
import org.xlp.base.application.base.Version;
import org.xlp.base.application.dao.ApplicationBaseDaoHelper;
import org.xlp.db.exception.EntityException;
import org.xlp.db.sql.UpdateSQL;
import org.xlp.mv.BaseService;

import java.io.Serializable;
import java.util.List;

/**
 * Create by xlp on 2022/6/12
 */
public class ApplicationBaseService extends BaseService {
    ApplicationBaseService(){}

    @Override
    public <T> int update(T bean) {
        if (bean instanceof CreatedInfo){
            ((CreatedInfo) bean).setUpdateTime(System.currentTimeMillis());
        }
        UpdateSQL<T> updateSQL = new UpdateSQL<>(bean, false);
        if (bean instanceof Version){
            Version version = (Version) bean;
            updateSQL.andEq(version.getVersionFiledName(), version.getVersion());
        }
        return update(updateSQL);
    }

    @Override
    public <T> Object save(T bean) {
        if (bean instanceof CreatedInfo){
            long currentTime = System.currentTimeMillis();
            ((CreatedInfo) bean).setUpdateTime(currentTime);
            ((CreatedInfo) bean).setCreateTime(currentTime);
        }
        return super.save(bean);
    }

    /**
     * 向数据库中插入多条数据
     * <p>
     * 假如给定要存储的对象是实体，并有相关的主键，并主键值未指定，那么此函数会自动生成一个可用的主键值
     *
     * @param beanList 数据集合
     * @return
     * @throws EntityException OptionDBException
     *                         假如该对象不是实体或数据库访问出错时，抛出该异常
     */
    @Override
    public <T> int[] save(List<T> beanList) {
        for (T bean : beanList) {
            if (bean instanceof CreatedInfo){
                long currentTime = System.currentTimeMillis();
                ((CreatedInfo) bean).setUpdateTime(currentTime);
                ((CreatedInfo) bean).setCreateTime(currentTime);
            }
        }
        return BaseServiceHelper.getBatchBaseService().save(beanList);
    }

    /**
     * 批量更新，指定的bean一定要主键属性
     *
     * @param beanList
     * @return
     * @throws EntityException OptionDBException
     *                         假如该对象没要有相关的主属性或不是实体或数据库访问出错时，抛出该异常
     */
    @Override
    public <T> int[] update(List<T> beanList) {
        for (T bean : beanList) {
            if (bean instanceof CreatedInfo){
                ((CreatedInfo) bean).setUpdateTime(System.currentTimeMillis());
            }
        }
        return BaseServiceHelper.getBatchBaseService().update(beanList);
    }

    /**
     * 向数据库中插入多条数据
     * <p>
     * 假如给定要存储的对象是实体，并有相关的主键，并主键值未指定，那么此函数会自动生成一个可用的主键值
     *
     * @param beanList 数据集合
     * @return
     * @throws EntityException OptionDBException
     *                         假如该对象不是实体或数据库访问出错时，抛出该异常
     */
    <T> int[] _save(List<T> beanList) {
        return super.save(beanList);
    }

    /**
     * 批量更新，指定的bean一定要主键属性
     *
     * @param beanList
     * @return
     * @throws EntityException OptionDBException
     *                         假如该对象没要有相关的主属性或不是实体或数据库访问出错时，抛出该异常
     */
    <T> int[] _update(List<T> beanList) {
        return super.update(beanList);
    }

    /**
     * 批量删除
     * @param entityClass
     * @param ids
     * @param <T>
     * @return
     */
    public <T> int deleteBatch(Class<T> entityClass, Serializable[] ids){
        return ApplicationBaseDaoHelper.getApplicationBaseDao().deleteBatch(entityClass, ids);
    }

    /**
     * 批量删除
     * @param entityClass
     * @param ids
     * @param <T>
     * @return
     */
    public <T> int deleteBatch(Class<T> entityClass,List<Serializable> ids){
        return deleteBatch(entityClass, ids.toArray(new Serializable[0]));
    }
}
